<template>
  <el-dialog title="修改设备" :visible="isShow" width="630px" @close="closeFn">
    <el-form ref="vmStatus" :rules="rules" :model="vmStatus">
      <el-form-item label="机器编号：" label-width="150px">
        <span>{{vmStatus.innerCode}}</span>
      </el-form-item>
      <el-form-item label="供货时间：" label-width="150px">
        <span>{{vmStatus.lastSupplyTime.split('T').join(' ')}}</span>
      </el-form-item>
      <el-form-item label="设备类型：" label-width="150px">
        <span>{{vmStatus.type.name}}</span>
      </el-form-item>
      <el-form-item label="设备容量：" label-width="150px">
        <span>{{vmStatus.type.vmCol}}</span>
      </el-form-item>
      <el-form-item label="设备点位:" label-width="150px" prop="nodeId">
       <el-select
          style="width: 85%"
          v-model="vmStatus.node.id"
          placeholder="请选择"
        >
          <el-option
            v-for="item in nodeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
       <el-form-item label="合作商：" label-width="150px">
        <span>{{vmStatus.ownerName}}</span>
      </el-form-item>
       <el-form-item label="所属区域：" label-width="150px">
        <span>{{vmStatus.region.name}}</span>
      </el-form-item>
       <el-form-item label="设备地址：" label-width="150px">
        <span>{{vmStatus.node.addr.split('-').splice(3).join('')}}</span>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button
            class="btn cancel"
            @click="closeFn"
            style="margin-right: 20px"
            >取消</el-button
          >
          <el-button class="btn confirm" type="primary" @click="submit"
            >确认</el-button
          >
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { editVmNodeApi } from "@/api/vm";
import { searchNodeApi } from "@/api/node";
export default {
  props: {
    isShow: Boolean,
    vmNow: Object,
  },
  data() {
    return {
      nodeList:[],
      vmStatus:{},
      pageParams:{
        pageIndex:1,
        pageSize:1000
      },
      rules: {
        nodeId: [{ required: true, message: "请选择点位", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getNodeList()
    this.vmStatus = this.vmNow
  },
  methods: {
    async getNodeList(){
      const res = await searchNodeApi(this.pageParams)
      this.nodeList = res.currentPageRecords
    },
    closeFn() {
      this.$refs.vmStatus.resetFields();
      this.vmStatus = {};
      this.$emit("update:isShow", false);
    },
    submit() {
      this.$refs.vmStatus.validate(async (pass) => {
        if (pass) {
            await editVmNodeApi({id:this.vmStatus.id,nodeId:this.vmStatus.node.id});
            this.$message.success("修改设备点位成功");
            this.closeFn();
            this.$emit('searchVm')
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.btn {
  width: 80px;
  height: 36px;
  border: none;
  padding: 0;
  line-height: 36px;
}
.cancel {
  background-color: #fbf4f0;
  color: #7e7570;
}
.confirm {
  background-color: #ff6d29;
  color: #fff;
}
</style>
